package com.fourth.shopping.controller;



import com.fourth.shopping.commons.Code;
import com.fourth.shopping.dto.GoodsTableSearch;
import com.fourth.shopping.entity.Goods;
import com.fourth.shopping.entity.Item;
import com.fourth.shopping.service.GoodsService;

import com.fourth.shopping.vo.Page4Vue;
import com.fourth.shopping.vo.PageCondition;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Resource
    private GoodsService goodsService;


    @GetMapping("/getGoodsByLimit.do")
    public Page4Vue<GoodsTableSearch> getGoodsByLimit(Goods goods, PageCondition pageCondition) {
        List<GoodsTableSearch> goodsByLimit = goodsService.getGoodsByLimit(goods, pageCondition);

        Integer goodsCountByLimit = goodsService.getGoodsCountByGoodId(goods);

        return new Page4Vue<GoodsTableSearch>(goodsByLimit, goodsCountByLimit);
    }


    @PostMapping("/changeGoodsStatus.do")
    public Code deleteGoods(String goodsId) {
        Integer integer = goodsService.deleteGoods(goodsId);
        Code code = new Code(0, null);
        if (integer > 0) {
            code.setMsg("success");
            code.setCode(integer);
        } else {
            code.setMsg("defeated");
            code.setCode(integer);
        }
        return code;
    }


    @PostMapping("/addGoods")
    public Code addGoods(Goods goods) {

        Integer integer = goodsService.addGoods(goods);

        if (integer > 0) {
            return new Code(integer,"success");
        } else {
            return new Code(integer,"defeated");
        }

    }


    /**
     * 返回数据库所有商品种类
     *
     * @return
     */
    @GetMapping("/getGoodsAllType")
    public List<Item> getGoodsAllType() {

        return goodsService.getGoodsAllType();
    }


    @PostMapping("/uploadDispalyImage")
    public Map uploadImage(MultipartFile displayImage, String itemAlias) {
        String imageName = displayImage.getOriginalFilename();
        System.out.println("文件上传商品信息:" + imageName);
        System.out.println("文件上传商品类型:" + itemAlias);
        HashMap<String, String> hashMap = new HashMap<>();
        //文件上传最终路径
        String root = "E:\\nginx-1.17.2\\images\\";
        try {
            displayImage.transferTo(new File(root+itemAlias+"\\"+imageName));
        } catch (IOException e) {
            System.out.println("文件上传失败");
        }
//        data.imageUrl,_data.filePath

        hashMap.put("filePath",new String(itemAlias+"/"+imageName));
        hashMap.put("imageUrl",new String("http://10.3.140.185/"+itemAlias+"/"+imageName));

        return hashMap ;
    }
}
